from django.utils.translation import ugettext as _
from oscar.apps.basket.abstract_models import AbstractBasket, AbstractLine
from oscar.templatetags.currency_filters import currency

class Basket(AbstractBasket):
    pass

    def add_product(self, product, price, quantity, options):
        """
        Add a product to the basket

        The 'options' list should contains dicts with keys 'option' and 'value'
        which link the relevant product.Option model and string value
        respectively.
        """
        if options is None:
            options = []
        if not self.id:
            self.save()

        # Line reference is used to distinguish between variations of the same
        # product (eg T-shirts with different personalisations)
        line_ref = self._create_line_reference(product, options)

        # Determine price to store (if one exists).  It is only stored for
        # audit and sometimes caching.
        price_excl_tax, price_incl_tax = None, None
        #if product.has_stockrecord:
            #stockrecord = product.stockrecord
            #if stockrecord:
                #price_excl_tax = getattr(stockrecord, 'price_excl_tax', None)
                #price_incl_tax = getattr(stockrecord, 'price_incl_tax', None)

        line, created = self.lines.get_or_create(
            line_reference=line_ref,
            product=product,
            defaults={'quantity': quantity,
                      'price_excl_tax': price,
                      'price_incl_tax': price})
        if created:
            for option_dict in options:
                line.attributes.create(option=option_dict['option'],
                                       value=option_dict['value'])
        else:
            line.quantity += quantity
            line.save()
        self.reset_offer_applications()
    add_product.alters_data = True

class Line(AbstractLine):
    pass

    @property
    def unit_price_incl_tax(self):
        """Return unit price including tax"""
        return self.price_incl_tax
        #if not self._charge_tax:
            #return self.unit_price_excl_tax
        #return self._get_stockrecord_property('price_incl_tax')

    def get_warning(self):
        """
        Return a warning message about this basket line if one is applicable

        This could be things like the price has changed
        """
        if not self.price_incl_tax:
            return
        if not self.product.has_stockrecord:
            msg = u"'%(product)s' is no longer available"
            return _(msg) % {'product': self.product.get_title()}

        current_price_incl_tax = self.product.stockrecord.price_incl_tax
        if current_price_incl_tax > self.price_incl_tax:
            msg = ("The price of '%(product)s' has increased from "
                   "%(old_price)s to %(new_price)s since you added it "
                   "to your basket")
            return _(msg) % {
                'product': self.product.get_title(),
                'old_price': currency(self.price_incl_tax),
                'new_price': currency(current_price_incl_tax)}
        #if current_price_incl_tax < self.price_incl_tax:
            #msg = ("The price of '%(product)s' has decreased from "
                   #"%(old_price)s to %(new_price)s sinceAAAA you added it "
                   #"to your basket")
            #return _(msg) % {
                #'product': self.product.get_title(),
                #'old_price': currency(self.price_incl_tax),
                #'new_price': currency(current_price_incl_tax)}

from oscar.apps.basket.models import *
